﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class DuyePhieuDeNghiMuaHang : UserControl
    {
        public DuyePhieuDeNghiMuaHang()
        {
            InitializeComponent();
        }

        private void DuyetPhieuMuaHang1_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            dgvPhieuDeNghi.Rows.Clear();
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            var listPhieu = from qt in dc.QuyTrinhMuaHangs
                            where qt.deleted == false &&
                                  qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_DE_NGHI
                            orderby qt.PhieuDeNghiMuaHang.NgayTao descending
                            select qt.PhieuDeNghiMuaHang;

            int count = 1;
            foreach (_DB.PhieuDeNghiMuaHang p in listPhieu)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvPhieuDeNghi);

                row.Cells[0].Value = count++;
                row.Cells[1].Value = p.MaPhieu;
                row.Cells[2].Value = p.NgayTao;
                row.Cells[3].Value = p.NguoiTao.Ten;
                row.Cells[4].Value = p.PhieuDeNghiMuaHang_HangHoas.Count;
                dgvPhieuDeNghi.Rows.Add(row);
            }
        }


        private void btnDuyet_Click(object sender, EventArgs e)
        {
            // Check
            if (dgvPhieuDeNghi.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show("Chắn chắn duyệt?", "Xác nhận",
                                MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // save
            string maPhieu = (string)dgvPhieuDeNghi.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang q = dc.QuyTrinhMuaHangs.Single(qt => qt.MaPhieuDeNghiMuaHang == maPhieu);
            q.PhieuDeNghiMuaHang.MaNguoiDuyet = Program.MaNguoiDungHienTai;
            q.PhieuDeNghiMuaHang.NgayDuyet = DateTime.Now;
            q.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DA_DUYET_PHIEU_DE_NGHI;
            dc.SubmitChanges();
            loadData();
        }

        private void btnKhongDuyet_Click(object sender, EventArgs e)
        {
            // Check
            if (dgvPhieuDeNghi.SelectedRows.Count <= 0)
                return;

            // Input ly do
            OtherForms.InputTextForm tf = new OtherForms.InputTextForm("", "Lý do không duyệt");
            tf.ShowDialog();
            if (tf.OK == false)
                return;

            string lydo = tf.getText();

            // save
            string maPhieu = (string)dgvPhieuDeNghi.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang q = dc.QuyTrinhMuaHangs.Single(qt => qt.MaPhieuDeNghiMuaHang == maPhieu);
            q.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_DE_NGHI;
            q.PhieuDeNghiMuaHang.LyDoKhongDuyet = lydo;
            q.PhieuDeNghiMuaHang.MaNguoiDuyet = Program.MaNguoiDungHienTai;
            q.PhieuDeNghiMuaHang.NgayDuyet = DateTime.Now;
            dc.SubmitChanges();
            loadData();
        }

        private void btnReport_Click(object sender, EventArgs e)
        {

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // Check
            if (dgvPhieuDeNghi.SelectedRows.Count <= 0)
                return;

            // open form
            string maPhieu = (string)dgvPhieuDeNghi.SelectedRows[0].Cells[1].Value;
            OtherForms.ThemPhieuDeNghiMuaHangForm f = new OtherForms.ThemPhieuDeNghiMuaHangForm(maPhieu);
            f.ShowDialog();
            loadData();
        }

        private void dgvPhieuMH_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuDeNghi.SelectedRows.Count > 0)
            {
                string maPhieu = (string)dgvPhieuDeNghi.SelectedRows[0].Cells[1].Value;
                OtherForms.XemPhieuDeNghiMuaHangForm f = new OtherForms.XemPhieuDeNghiMuaHangForm(maPhieu);
                f.Show();
            }
        }

    }
}
